package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.StudentLeave;
import com.ruoyi.system.service.IStudentLeaveService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请假记录Controller
 * 
 * @author ruoyi
 * @date 2023-03-29
 */
@Controller
@RequestMapping("/system/leave")
public class StudentLeaveController extends BaseController
{
    private String prefix = "system/leave";

    @Autowired
    private IStudentLeaveService studentLeaveService;

    @RequiresPermissions("system:leave:view")
    @GetMapping()
    public String leave()
    {
        return prefix + "/leave";
    }

    /**
     * 查询请假记录列表
     */
    @RequiresPermissions("system:leave:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StudentLeave studentLeave)
    {
        startPage();
        List<StudentLeave> list = studentLeaveService.selectStudentLeaveList(studentLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假记录列表
     */
    @RequiresPermissions("system:leave:export")
    @Log(title = "请假记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StudentLeave studentLeave)
    {
        List<StudentLeave> list = studentLeaveService.selectStudentLeaveList(studentLeave);
        ExcelUtil<StudentLeave> util = new ExcelUtil<StudentLeave>(StudentLeave.class);
        return util.exportExcel(list, "请假记录数据");
    }

    /**
     * 新增请假记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存请假记录
     */
    @RequiresPermissions("system:leave:add")
    @Log(title = "请假记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StudentLeave studentLeave)
    {
        return toAjax(studentLeaveService.insertStudentLeave(studentLeave));
    }

    /**
     * 修改请假记录
     */
    @RequiresPermissions("system:leave:edit")
    @GetMapping("/edit/{studentLeaveId}")
    public String edit(@PathVariable("studentLeaveId") Long studentLeaveId, ModelMap mmap)
    {
        StudentLeave studentLeave = studentLeaveService.selectStudentLeaveByStudentLeaveId(studentLeaveId);
        mmap.put("studentLeave", studentLeave);
        return prefix + "/edit";
    }

    /**
     * 修改保存请假记录
     */
    @RequiresPermissions("system:leave:edit")
    @Log(title = "请假记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StudentLeave studentLeave)
    {
        return toAjax(studentLeaveService.updateStudentLeave(studentLeave));
    }

    /**
     * 删除请假记录
     */
    @RequiresPermissions("system:leave:remove")
    @Log(title = "请假记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(studentLeaveService.deleteStudentLeaveByStudentLeaveIds(ids));
    }
}
